#Lab 1 In this lab I want to examine the mean temperatures in the countries of the world

I need these packages

#install.packages("rlang")
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.1
library(sf)
## Warning: package 'sf' was built under R version 4.2.1
## Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.1
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.2      ✔ forcats 0.5.2 
## ✔ purrr   0.3.4
## Warning: package 'tibble' was built under R version 4.2.1
## Warning: package 'tidyr' was built under R version 4.2.1
## Warning: package 'readr' was built under R version 4.2.1
## Warning: package 'purrr' was built under R version 4.2.1
## Warning: package 'dplyr' was built under R version 4.2.1
## Warning: package 'stringr' was built under R version 4.2.1
## Warning: package 'forcats' was built under R version 4.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(ggspatial)
## Warning: package 'ggspatial' was built under R version 4.2.1
library(viridis)
## Warning: package 'viridis' was built under R version 4.2.1
## Loading required package: viridisLite
## Warning: package 'viridisLite' was built under R version 4.2.1
  1. Data: I retrieved global mean temperature raster dataset from climate resource portal of the world bank. I then ran the zonal statistics tool with world countries shapefile. This way all the countries were the zone data and the temperature raster was the input shapefile, these zones captured the mean temperatures from the rasters and that data was used to create the visualization.
temp <- st_read("D:/geomatics/umich/Sem1/geoviz/week3/temp_shp.shp")
## Reading layer `temp_shp' from data source 
##   `D:\geomatics\umich\Sem1\geoviz\week3\temp_shp.shp' using driver `ESRI Shapefile'
## Simple feature collection with 256 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -180 ymin: -58.49861 xmax: 180 ymax: 83.6236
## Geodetic CRS:  WGS 84
head(temp)
## Simple feature collection with 6 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -25.36056 ymin: -18.07492 xmax: 115.3603 ymax: 58.08326
## Geodetic CRS:  WGS 84
##               region continent                 name tempmean
## 1    Southern Europe    Europe           San Marino 16.64927
## 2       Western Asia      Asia Syrian Arab Republic 25.52712
## 3    Northern Europe    Europe               Latvia 10.66914
## 4     Western Africa    Africa           Cape Verde 25.40291
## 5     Eastern Africa    Africa               Zambia 29.25888
## 6 South-Eastern Asia      Asia    Brunei Darussalam 31.10000
##                         geometry
## 1 MULTIPOLYGON (((12.40913 43...
## 2 MULTIPOLYGON (((42.35562 37...
## 3 MULTIPOLYGON (((27.37206 57...
## 4 MULTIPOLYGON (((-24.36556 1...
## 5 MULTIPOLYGON (((32.9404 -9....
## 6 MULTIPOLYGON (((115.0291 4....

Table region: where in the continent continent: name of the continent name: name of the county tempmean: Mean temperatures of the country

  1. We can plot these variables using ggplot2. We define the sf data using the geom_sf function. The different arguments control the object attributes(this can be points, lines or polygons). For example, fill= control the color of object outline. alpha = controls the opacity of the object. The final argument is a complete theme, which controls the non-data display(e.g. neatlines, gradicule title). More details can be found regarding these [themes] here(https://ggplot2.tidyverse.org/reference/ggtheme.html). Here we use theme_bw, which is the black and white theme. You can try other themes to explore the different options.
ggplot() +
    geom_sf(data = temp,
    fill = NA, alpha = .2) +
    theme_bw()

3. Now we will explore different methods for visualizing this data. We will add ‘Gradient colour scales’ in ggplot2. Here is the documentation of these options https://ggplot2.tidyverse.org/reference/scale_gradient.html

ggplot() +
    geom_sf(data =temp, aes(fill=tempmean)) + labs(fill="Global men temperatures")+
  geom_sf(color = NA) +
    theme_bw() + scale_fill_viridis_c(option = "turbo")

4. using different color schemes to visualize the mean temperature data.

ggplot() +
    geom_sf(data =temp, aes(fill=tempmean)) + labs(fill="Global men temperatures")+
    theme_bw() + scale_fill_gradientn(colors  = viridis(8))

  1. Let’s look at the locations above 22 degrees celcius. For this we will need to use the ifelse() function. The function basically means if the first argument is true (tempmean >= 22), the mean temperature is greater than 22 degree celcius, then print the first varible: TRUE; if not true, print the second varible: FALSE. We use the mutate fucntion to make a new variable in our boulder dataframe. We then use ggplot to plot these locations
temp %>%
    mutate(tempmean = ifelse(tempmean >= 22, TRUE, FALSE))%>% 
ggplot() +
  geom_sf(aes(fill=tempmean), alpha = .2)  +  
  theme_bw()

6. We can also plot different charts using ggplot. Let’s compare the average temperature of all the continents. Here we filter() to analyze the data.

temp %>%
  filter(continent ==  'Asia' | continent == 'Europe' |continent == 'Africa' | continent =='Oceania' | continent=='Americas' ) %>%
  ggplot(aes(x=continent, y=tempmean
             )) + 
  geom_boxplot()
## Warning: Removed 3 rows containing non-finite values (stat_boxplot).

7. Using tmap to plot data

library(tmap)
## Warning: package 'tmap' was built under R version 4.2.1
## Add the data - these are specific to the vector or raster
tm_shape(temp) + 
  ## which variable, is there a class interval, palette, and other options
  tm_fill(col='tempmean', 
             style='quantile', 
             palette = 'YlOrRd',
             border.lwd = NA,
             size = 0.1)

8. interactive map using tmap

tmap_mode('view')
## tmap mode set to interactive viewing
tm_shape(temp) + 
  ## which variable, is there a class interval, palette, and other options
  tm_fill(col='tempmean', 
             style='quantile', 
             palette = 'YlOrRd',
             border.lwd = NA,
             size = 0.1)